﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Joyintech.Common;
using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;
using System.Data;

namespace Joyintech.Admin.inpiao365.general.sms.sendbak
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //绑定列表
                DataBindMsgRecordBak();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DataBindMsgRecordBak();
        }

        private void DataBindMsgRecordBak()
        {
            StringBuilder strSql = new StringBuilder();

            if (this.txtMsgSendTelphone.Text.Trim() != "")
            {
                strSql.Append(" and MsgSendTelphone like '%" + this.txtMsgSendTelphone.Text.Trim() + "%'");
            }

            if (this.txtMsgContent.Text.Trim() != "")
            {
                strSql.Append(" and MsgContent like '%" + this.txtMsgContent.Text.Trim() + "%'");
            }

            if (this.txtMsgSendTimeStart.Text.Trim() != "")
            {
                strSql.Append(" and MsgSendTime>='" + this.txtMsgSendTimeStart.Text.Trim() + "'");
            }

            if (this.txtMsgSendTimeEnd.Text.Trim() != "")
            {
                strSql.Append(" and MsgSendTime<='" + this.txtMsgSendTimeEnd.Text.Trim() + "'");
            }

            this.SqlPager1.CurrentPageIndex = 0;
            this.SqlPager1.ConnStringKeyName = "localServer";
            this.SqlPager1.SelectCommand = "a.*,b.UserTrueName";
            this.SqlPager1.SelectCommandTable = "MsgRecordBak a,SystemUser b";
            this.SqlPager1.SelectCommandWhere = "a.UserId=b.UserId" + strSql.ToString();
            this.SqlPager1.SortField = "MsgSendTime";
            this.SqlPager1.SortStyle = WebControls.SortMode.DESC;
            this.SqlPager1.DataBind();
        }


        #region 导出当前页

        protected void btnExportCurrentPage_Click(object sender, EventArgs e)
        {
            DataTable dt = this.SqlPager1.CurrentPageDataTable;

            if (dt != null && dt.Rows.Count > 0)
            {
                OutExcel(dt, "UserId_" + systemUser_BLL.GetInstance().GetCurrentSystemUser().UserId.ToString() + "_MsgRecordBak_" + this.SqlPager1.CurrentPageIndex);
            }
            else
            {
                MessageBox.Show(this, "没有数据可供导出！");
            }
        }

        #endregion

        #region 导出全部

        protected void btnExportAll_Click(object sender, EventArgs e)
        {
            DataTable dt = this.SqlPager1.CachePageData;

            if (dt != null && dt.Rows.Count > 0)
            {
                OutExcel(dt, "UserId_" + systemUser_BLL.GetInstance().GetCurrentSystemUser().UserId.ToString() + "_MsgRecordBak");
            }
            else
            {
                MessageBox.Show(this, "没有数据可供导出！");
            }
        }

        #endregion

        #region 导出Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="dg"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        private void DataGridToExcel(DataGrid dg, string name, string type)
        {
            dg.Visible = true;
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.HtmlEncode(name) + ".xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = type;
            dg.EnableViewState = false;
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            dg.RenderControl(oHtmlTextWriter);
            Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\">");
            Response.Write(oStringWriter.ToString());
            Response.End();
        }

        /// <summary>
        /// DataTable导出Excel
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="name"></param>
        private void OutExcel(DataTable dt, string name)
        {
            DataGrid dg = new DataGrid();

            dg.DataSource = dt;
            dg.DataBind();

            //DataGrid导出Excel
            DataGridToExcel(dg, name, "application/vnd.ms-excel");
        }

        #endregion
    }
}